package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.PtSysVideoBo;
import com.ruoyi.system.domain.vo.PtSysVideoVo;
import com.ruoyi.system.domain.PtSysVideo;
import com.ruoyi.system.mapper.PtSysVideoMapper;
import com.ruoyi.system.service.IPtSysVideoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-26
 */
@Service
public class PtSysVideoServiceImpl extends ServicePlusImpl<PtSysVideoMapper, PtSysVideo, PtSysVideoVo> implements IPtSysVideoService {

    @Override
    public PtSysVideoVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PtSysVideoVo> queryPageList(PtSysVideoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PtSysVideo> lqw = buildQueryWrapper(bo);
        Page<PtSysVideoVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<PtSysVideoVo> queryList(PtSysVideoBo bo) {
        LambdaQueryWrapper<PtSysVideo> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<PtSysVideo> buildQueryWrapper(PtSysVideoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PtSysVideo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpId() != null, PtSysVideo::getExpId, bo.getExpId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), PtSysVideo::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), PtSysVideo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getIntro()), PtSysVideo::getIntro, bo.getIntro());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PtSysVideoBo bo) {
        PtSysVideo add = BeanUtil.toBean(bo, PtSysVideo.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(PtSysVideoBo bo) {
        PtSysVideo update = BeanUtil.toBean(bo, PtSysVideo.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PtSysVideo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
